package com.hong.spring.common;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


/**
 * @author: csh
 * @Date: 2021/4/19 16:21
 * @Description:mq工具类
 */
@Service
@Log4j2
public class MqUtils {
    @Autowired
    private JmsTemplate jmsTemplate;
    /**
     * 向指定Destination(队列)发送text消息
     *
     * @param destination
     * @param message
     */
    public void sendTxtMessage(Destination destination, final String message) {
        if (null == destination) {
            destination =  jmsTemplate.getDefaultDestination();
        }
        jmsTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                log.info("发送的消息{}",message);
               return session.createTextMessage(message);
            }
        });

    }
}
